package com.hoshiicloud.user.controller.company;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.validate.ValidGroupAdd;
import com.hoshiicloud.common.validate.ValidGroupUpdate;
import com.hoshiicloud.user.entity.company.OrganizationType;
import com.hoshiicloud.user.param.platform.OrganizationTypeParam;
import com.hoshiicloud.user.service.company.IOrganizationTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 机构类型 前端控制器
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Slf4j
@RestController
@RequestMapping("/company/orgtype")
@Api(value = "机构类型管理", tags = {"机构类型操作接口"})
public class OrganizationTypeController {
    @Autowired
    private IOrganizationTypeService orgTypeService;

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询附件信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询",
            notes = "查询机构类型信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response findOrgtypeById(@PathVariable("id")  Long id) {

        return Response.data(orgTypeService.getById(id));
    }

	 /**
     * 根据ID组查询机构类型信息
     * @param ids ID组
     * @return Response<List<OrganizationType>>
     */
    @GetMapping("/getByIds")
    @ApiOperation(value = "根据ID组查询", notes = "根据ID组查询机构类型信息", consumes = MediaType.ALL_VALUE)
    public Response<List<OrganizationType>> getByIds(@RequestParam("ids") List<Long> ids) {
        Response<List<OrganizationType>> rs;
        try {
            rs = Response.data(orgTypeService.getByIds(ids));
        } catch (Exception e) {
            rs = Response.failed("根据ID组查询机构类型信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 根据名称查询
     * @param name 机构名称
     * @return Response<List<OrganizationType>>
     */
    @GetMapping("/find/{name}")
    @ApiOperation(value = "查询", notes = "根据名称查询机构类型信息", consumes = MediaType.ALL_VALUE)
    public Response findByName(@PathVariable("name") String name) {
        Response rs;
        try {
            Wrapper wrapper = Wrappers.<OrganizationType>query().lambda().like(OrganizationType::getOrgName, name);
            rs = Response.data(orgTypeService.list(wrapper));
        } catch (Exception e) {
            rs = Response.failed("根据名称查询机构类型信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 分页查询机构类型信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response<IPage<OrganizationType>>
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询机构类型信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<IPage<OrganizationType>> list(@RequestBody OrganizationTypeParam param) {
        try {
            QueryWrapper<OrganizationType> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getOrgCode()!= null), OrganizationType::getOrgCode, param.getOrgCode())
                    .eq((param.getOrgName()!= null), OrganizationType::getOrgName, param.getOrgName())
                    .eq((param.getOrgType() != null),  OrganizationType::getOrgType, param.getOrgType());
            IPage<OrganizationType> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
            return Response.data(orgTypeService.getPage(page,queryWrapper,key));
        } catch (Exception e) {
            log.error("分页查询租户信息异常", e);
            return Response.failed("分页查询租户信息异常");
        }
    }

    /**
     * @param organizationType
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增",
            notes = "新增机构类型信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response saveOrgtype(@RequestBody @Validated(value = ValidGroupAdd.class) OrganizationType organizationType) {

        return Response.data(orgTypeService.save(organizationType));
    }

    /**
     * @param organizationType
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改",
            notes = "修改机构类型信息",
            produces = "application/json, application/rbac",
            consumes = "application/json, application/rbac")
    public Response updateOrgtypeById(@RequestBody @Validated(value = ValidGroupUpdate.class) OrganizationType organizationType) {
        return Response.data(orgTypeService.updateById(organizationType));

    }

    /**
     * 根据ID删除机构类型信息
     * @param id 机构类型ID
     * @return Response<Boolean>
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除机构类型信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "机构类型ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        return Response.data(orgTypeService.delete(id));
    }

    /**
     *@Author:campchen
     *@Description:
     *@Date:下午 02:49 2019/9/11
     *@Param:
     *@return:
     */
    @GetMapping("/query/{orgType}")
    @ApiOperation(value = "全量查询", notes = "全量查询")
    public Response selectAll(@PathVariable("orgType") Integer orgType){
        return Response.data(orgTypeService.selectAll(orgType));
    }

}
